﻿namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    /// Calculator for calculating the appropriate font size for a given text area.
    /// </summary>
    public class FontSizeCalculator
    {
        private readonly float _textAreaWidth;

        /// <summary>
        /// Initializes a new instance of the <see cref="FontSizeCalculator"/> class.
        /// </summary>
        /// <param name="textAreaWidth">Width of the text area.</param>
        public FontSizeCalculator(float textAreaWidth)
        {
            _textAreaWidth = textAreaWidth;
        }

        /// <summary>
        /// Gets the size of the font.
        /// </summary>
        /// <returns></returns>
        public Length GetFontSize()
        {

            return new Length
                       {
                           Unit = LengthUnit.Point,
                           Value = DetermineFontSize(_textAreaWidth)
                       };
        }

        private static float DetermineFontSize(double widthInPixels)
        {
            float fontSize;

            if (widthInPixels >= new Inch(9.75).ToPixels())
                fontSize = 30;
            else if (widthInPixels >= new Inch(8.5).ToPixels())
                fontSize = 26;
            else if (widthInPixels >= new Inch(7).ToPixels())
                fontSize = 21;
            else if (widthInPixels >= new Inch(6).ToPixels())
                fontSize = 18;
            else if (widthInPixels >= new Inch(5).ToPixels())
                fontSize = 16;
            else if (widthInPixels >= new Inch(4).ToPixels())
                fontSize = 12;
            else
                fontSize = 30;

            return fontSize;
        }
        
    }
}